<template>
  <div class="LoginForm-wrap">
    <el-form
      ref="loginForm"
      :model="loginForm"
      :rules="rules"
      @keydown.enter.prevent
      @keyup.enter.native="handleLogin"
    >
      <el-form-item prop="userName" style="margin-top: 40px">
        <el-input
          v-model.trim="loginForm.userName"
          placeholder="请输入用户名"
          prefix-icon="el-icon-user"
          tabindex="1"
          type="text"
        />
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model.trim="loginForm.password"
          placeholder="请输入密码"
          prefix-icon="el-icon-lock"
          show-password
          tabindex="2"
        />
      </el-form-item>
    </el-form>
    <div class="login-btn">
      <el-button
        icon="el-icon-circle-close"
        round
        size="medium"
        @click="resetForm(['userName', 'password'])"
      >
        重 置
      </el-button>
      <el-button
        icon="el-icon-user"
        round
        size="medium"
        type="info"
        :loading="loading"
        @click="handleLogin"
      >
        登 录
      </el-button>
    </div>
  </div>
</template>
<script>
import { validUsername } from "@/utils/validate";

export default {
  name: "LoginForm",
  data() {
    const validateUsername = (rule, value, callback) => {
      if (!validUsername(value)) {
        callback(new Error("请输入正确的用户名!"));
      } else {
        callback();
      }
    };
    const validatePassword = (rule, value, callback) => {
      if (value.length < 5) {
        callback(new Error("密码不能少于5位!"));
      } else {
        if (value === "admin") {
          callback();
        } else {
          callback(new Error("请输入正确的密码!"));
        }
      }
    };
    return {
      loginForm: {
        userName: "",
        password: "",
      },
      rules: {
        userName: [
          {
            required: true,
            trigger: "blur",
            validator: validateUsername,
          },
        ],
        password: [
          {
            required: true,
            trigger: "blur",
            validator: validatePassword,
          },
        ],
      },
      loading: false,
      redirect: undefined,
      codeUrl: "",
      otherQuery: {},
    };
  },
  watch: {
    $route: {
      handler: function (route) {
        const query = route.query;
        if (query) {
          this.redirect = query.redirect;
          this.otherQuery = this.getOtherQuery(query);
        }
      },
      immediate: true,
    },
  },
  mounted() {},
  methods: {
    handleLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          // this.loading = true
          this.$router.push("/");
        } else {
          return false;
        }
      });
    },
    getOtherQuery(query) {
      return Object.keys(query).reduce((acc, cur) => {
        if (cur !== "redirect") {
          acc[cur] = query[cur];
        }
        return acc;
      }, {});
    },
    resetForm(args) {
      for (const i of args) {
        this.loginForm[i] = null;
      }
    },
  },
};
</script>

<style scoped lang="scss">
@import "../index";
</style>
